{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
del, def, return, is keyword."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In Python version 2.7, `print` was keyword, now in Python 3 `print` is function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Namespace"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A global definition is always available until quit:"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"x=3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# globals() # try this and look for x in output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A function has its own local namespace:"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'x': 4, 'y': 2}\n"
]
},
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=3\n",
"y='whatever';del y\n",
"def function1(x):\n",
" y=2\n",
" print(locals())\n",
" return x+y\n",
" \n",
"function1(4)\n",
"\n",
"x\n",
"# y # error"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Default behaviour can be altered (but this may be confusing). From the Python tutorial:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"scrolled": true,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"After local assignment: test spam\n",
"After nonlocal assignment: nonlocal spam\n",
"After global assignment: nonlocal spam\n",
"In global scope after scope_test: global spam\n"
]
}
],
"source": [
"def scope_test():\n",
" def do_local():\n",
" spam = \"local spam\"\n",
"\n",
" def do_nonlocal():\n",
" nonlocal spam\n",
" spam = \"nonlocal spam\"\n",
" \n",
" def do_global():\n",
" global spam\n",
" spam = \"global spam\"\n",
" \n",
" spam = \"test spam\"\n",
" do_local()\n",
" print(\"After local assignment:\", spam)\n",
" do_nonlocal()\n",
" print(\"After nonlocal assignment:\", spam)\n",
" do_global()\n",
" print(\"After global assignment:\", spam)\n",
"\n",
"scope_test()\n",
"print(\"In global scope after scope_test:\", spam)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Module"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A module is a separate program unit, also with its own local namespace, accessed with a particular syntax using `.`:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n"
]
}
],
"source": [
"import keyword\n",
"print(keyword.kwlist) # all keywords"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2023, 7, 7, 19, 5, 9, 545034)"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import datetime\n",
"datetime.datetime.now()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# !pip install chime # if you don't have it\n",
"import chime \n",
"chime.error() # change number"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get all outputs (not only the last one) from the same program cell even after non final `;`:"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {
"init_cell": true,
"scene__Default Scene": true,
"tags": [
"ActiveScene"
]
},
"outputs": [],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity='all'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"3.4"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"289"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"17-2\n",
"17/5;\n",
"17//5\n",
"17%5\n",
"17**2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Importing into the global namespace:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.43491929890172687"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from random import random\n",
"random()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"0.2889314666773227"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Change something in a module:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.141592653589793"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"3.142857142857143"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"math.pi\n",
"math.pi=22/7\n",
"math.pi"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How to restore? import again does not work:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.142857142857143"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"del math\n",
"import math\n",
"math.pi"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This does not work:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[] replaces ():"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"factorial={0:1,1:1,2:2,3:6}\n",
"type(factorial)\n",
"factorial[1]\n",
"factorial[2]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"# hash(factorial) # error: non hashable"
]
},
{
"cell_type": "code",
"execution_count": 409,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 409,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type({}) # not empty set"
]
},
{
"cell_type": "code",
"execution_count": 235,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 1000, 1: 1, 2: 2, 3: 6}"
]
},
"execution_count": 235,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{0:1,1:1,2:2,3:6,0:1000}"
]
},
{
"cell_type": "code",
"execution_count": 237,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 237,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{0:1,1:1,2:2,3:6}=={0:1,3:6,1:1,2:2}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Namespace uses dictionary:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'__main__'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"globals()['__name__']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Algebra with `sympy`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Evaluating an undefined name produces an error, Python can't hold symbols:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# del x,y\n",
"# x # error\n",
"# x-x # error\n",
"# x=y # error"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hold symbolic expression as string and use substitution:"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [],
"source": [
"from re import match,sub"
]
},
{
"cell_type": "code",
"execution_count": 147,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"'y+x'"
]
},
"execution_count": 147,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"'z+y'"
]
},
"execution_count": 147,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sub(\"(.*)\\+(.*)\",r\"\\2+\\1\",\"x+y\")\n",
"sub(\"(.*)\\+(.*)\",r\"\\2+\\1\",\"y+z\")\n"
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'0'"
]
},
"execution_count": 148,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"'0'"
]
},
"execution_count": 148,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sub(\"(.*)-\\\\1\",\"0\",\"x-x\")\n",
"sub(\"(.*)-\\\\1\",\"0\",\"y-y\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Release hold with exec:"
]
},
{
"cell_type": "code",
"execution_count": 248,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 248,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"exec(sub('y','2',sub('x','1','sum1=x+y')))\n",
"sum1"
]
},
{
"cell_type": "code",
"execution_count": 249,
"metadata": {},
"outputs": [],
"source": [
"from sympy import var"
]
},
{
"cell_type": "code",
"execution_count": 250,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(x, y)"
]
},
"execution_count": 250,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var('x,y')"
]
},
{
"cell_type": "code",
"execution_count": 251,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"sympy.core.symbol.Symbol"
]
},
"execution_count": 251,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x)"
]
},
{
"cell_type": "code",
"execution_count": 252,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle x$"
],
"text/plain": [
"x"
]
},
"execution_count": 252,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/latex": [
"$\\displaystyle 0$"
],
"text/plain": [
"0"
]
},
"execution_count": 252,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x # LaTeX format in notebook output\n",
"x-x"
]
},
{
"cell_type": "code",
"execution_count": 253,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left(x + y\\right)^{3}$"
],
"text/plain": [
"(x + y)**3"
]
},
"execution_count": 253,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/latex": [
"$\\displaystyle x^{3} + 3 x^{2} y + 3 x y^{2} + y^{3}$"
],
"text/plain": [
"x**3 + 3*x**2*y + 3*x*y**2 + y**3"
]
},
"execution_count": 253,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/latex": [
"$\\displaystyle \\left(x + y\\right)^{3}$"
],
"text/plain": [
"(x + y)**3"
]
},
"execution_count": 253,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"expr=(x+y)**3\n",
"expr\n",
"\n",
"expr1=expr.expand()\n",
"expr1\n",
"\n",
"expr1.factor()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Procedural program"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variable"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A variable (name) is a name that can have different successive values in a program, as opposed to a constant (name). "
]
},
{
"cell_type": "code",
"execution_count": 263,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 263,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 263,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=2;x\n",
"x=3;x"
]
},
{
"cell_type": "code",
"execution_count": 264,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 264,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 264,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 264,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=0;x\n",
"x+=2;x\n",
"x*=3;x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A procedural program is a program using at least one variable. The debugging problem: what is the value of a variable at any time or location in a program?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mutable, hashable types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List:"
]
},
{
"cell_type": "code",
"execution_count": 265,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 100, 3]"
]
},
"execution_count": 265,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list1=[1,2,3]\n",
"list1[1]=100\n",
"list1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This works but is useless if the objects cannot be recalled:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"[1,2,3][1]=30"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mutable implies non hashable (hashable implies non mutable):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# hash(list1) # error: non hashable"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set:"
]
},
{
"cell_type": "code",
"execution_count": 267,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3, 4}"
]
},
"execution_count": 267,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set1={1,2,4}\n",
"set1|={3,4}\n",
"set1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dictionnary:"
]
},
{
"cell_type": "code",
"execution_count": 272,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Robert': '19450510', 'Géal': '19181111'}"
]
},
"execution_count": 272,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"birthdates={'Robert':'19450510','Géal':'19181111'}\n",
"birthdates"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Not only entries can be modified but entries can be added or deleted:"
]
},
{
"cell_type": "code",
"execution_count": 273,
"metadata": {},
"outputs": [],
"source": [
"birthdates['Robert']='19450511'\n",
"birthdates['Reynier']='18950405'"
]
},
{
"cell_type": "code",
"execution_count": 274,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Robert': '19450511', 'Reynier': '18950405'}"
]
},
"execution_count": 274,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"del birthdates['Géal']\n",
"birthdates"
]
},
{
"cell_type": "code",
"execution_count": 275,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Robert': '19450511', 'Reynier': '18950405'}"
]
},
"execution_count": 275,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"birthdates"
]
},
{
"cell_type": "code",
"execution_count": 280,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"tuple1=(1,2,3)\n",
"# tuple1[1]=4 # error"
]
},
{
"cell_type": "code",
"execution_count": 281,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"529344067295497451"
]
},
"execution_count": 281,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hash(tuple1) # hashable implies immutable"
]
},
{
"cell_type": "code",
"execution_count": 282,
"metadata": {},
"outputs": [],
"source": [
"str1=\"abc\"\n",
"# str1[1]='x' # error"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Immutable does not imply constant:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I have read Jeff Knupp https://jeffknupp.com."
]
},
{
"cell_type": "code",
"execution_count": 283,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(['Fred', 'George', 'Bill'], ['Smith', 'Jones', 'Williams'])"
]
},
"execution_count": 283,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"(['Fred', 'George', 'Bill', 'Igor'], ['Smith', 'Jones', 'Williams'])"
]
},
"execution_count": 283,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"first_names=['Fred','George','Bill']\n",
"last_names=['Smith','Jones','Williams']\n",
"name_tuple=(first_names,last_names) # tuple: immutable\n",
"name_tuple\n",
"first_names+=['Igor']\n",
"name_tuple"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However:"
]
},
{
"cell_type": "code",
"execution_count": 284,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"(['Fred', 'George', 'Bill'], ['Smith', 'Jones', 'Williams'])"
]
},
"execution_count": 284,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"(['Fred', 'George', 'Bill'], ['Smith', 'Jones', 'Williams'])"
]
},
"execution_count": 284,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"first_names=['Fred','George','Bill']\n",
"last_names=['Smith','Jones','Williams']\n",
"name_tuple=(first_names,last_names)\n",
"name_tuple\n",
"first_names=['Fred','George','Bill','Igor']\n",
"name_tuple"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Procedure"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A procedure is a function that has side effets, interacts with the environment apart from parameter list input and standard output (from `return`): \n",
"- uses as input a variable that is not a parameter,\n",
"\n",
"- touches a namespace other than its own, \n",
"\n",
"- prints, beeps, quits... \n",
"\n",
"Thus programming with procedure implies procedural programming.\n",
"\n",
"For example:"
]
},
{
"cell_type": "code",
"execution_count": 291,
"metadata": {},
"outputs": [],
"source": [
"def procedure(l,w,h):\n",
" global length,width,height\n",
" length=l\n",
" width=w\n",
" height=h\n",
" print(length*width*height)"
]
},
{
"cell_type": "code",
"execution_count": 292,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"62000\n"
]
}
],
"source": [
"procedure(31,40,50)"
]
},
{
"cell_type": "code",
"execution_count": 293,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"31"
]
},
"execution_count": 293,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"length"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`random` uses a seed variable that is not a parameter.\n",
"\n",
"`datetime` uses a clock variable that is not a parameter.\n",
"\n",
"`print` has non standard output."
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"## Pass by value or reference?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I have read Jeff Knupp https://jeffknupp.com. Pass is by name (object reference)."
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"If the argument is immutable, any attempt at changing it in the function will actually create an equivalent local variable, as if the argument was passed by value:"
]
},
{
"cell_type": "code",
"execution_count": 287,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"scrolled": true,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11\n"
]
},
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 287,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def ref_demo(x):x+=2;print(x)\n",
"\n",
"x=9 # 9 is immutable\n",
"ref_demo(x) \n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If the argument is mutable, it can be changed in the function, this is a side effect, as if the argument was passed by reference:"
]
},
{
"cell_type": "code",
"execution_count": 288,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 42]"
]
},
"execution_count": 288,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def ref_demo1(x):x[-1]=42\n",
" \n",
"list1=[1,2,3] \n",
"ref_demo1(list1)\n",
"list1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However:"
]
},
{
"cell_type": "code",
"execution_count": 290,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"execution_count": 290,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def ref_demo2(x):x=[1,2,42] # new object created\n",
" \n",
"list1=[1,2,3]\n",
"ref_demo2(list1)\n",
"list1\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Branching keywors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Match case"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"New in Python 3.10."
]
},
{
"cell_type": "code",
"execution_count": 294,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 294,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def factorial(n):\n",
" match n:\n",
" case 0:\n",
" return 1\n",
" case _: # anything\n",
" return n*factorial(n-1)\n",
"factorial(3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Try except"
]
},
{
"cell_type": "code",
"execution_count": 296,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"division by zero\n",
"but the program goes on\n"
]
}
],
"source": [
"x=0\n",
"try:1/x \n",
"except ZeroDivisionError:print(\"division by zero\")\n",
"print(\"but the program goes on\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercice. "
]
},
{
"cell_type": "code",
"execution_count": 300,
"metadata": {},
"outputs": [],
"source": [
"x=2\n",
"del x\n",
"# del x # error: NameError not define"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Undefine silently (without error message), even if name is not defined:"
]
},
{
"cell_type": "code",
"execution_count": 309,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"'x was deleted'"
]
},
"execution_count": 309,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=2\n",
"str1='x'\n",
"exec('del '+str1)\n",
"try:\n",
" x\n",
"except NameError:\n",
" \"x was deleted\""
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {
"init_cell": true,
"scene__Default Scene": true,
"tags": [
"ActiveScene"
]
},
"outputs": [],
"source": [
"def silentdel(str1):\n",
" try:\n",
" exec('del '+str1,globals())\n",
" except NameError:\n",
" pass # do nothing"
]
},
{
"cell_type": "code",
"execution_count": 303,
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [
{
"data": {
"text/plain": [
"'x was deleted'"
]
},
"execution_count": 303,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x='whatever'\n",
"silentdel('x')\n",
"try:x\n",
"except NameError:'x was deleted'\n",
"silentdel('x') # no error"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"x='whatever'\n",
"y='anything'\n",
"silentdel('x,y')\n",
"# x # error\n",
"# y # error\n",
"silentdel('x,y')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is simpler:"
]
},
{
"cell_type": "code",
"execution_count": 306,
"metadata": {},
"outputs": [],
"source": [
"def silentdel1(str1):\n",
" exec(str1+\"='whatever'\",globals())\n",
" exec('del '+str1,globals())"
]
},
{
"cell_type": "code",
"execution_count": 307,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'x was deleted'"
]
},
"execution_count": 307,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=1\n",
"silentdel1('x')\n",
"try:x\n",
"except NameError:'x was deleted'\n",
"silentdel1('x')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### If else elif"
]
},
{
"cell_type": "code",
"execution_count": 321,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5"
]
},
"execution_count": 321,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=2\n",
"if x!=0:\n",
" 1/x\n",
"else: \"division par zéro\""
]
},
{
"cell_type": "code",
"execution_count": 322,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'division par zéro'"
]
},
"execution_count": 322,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=0\n",
"if x!=0:1/x \n",
"else:\"division par zéro\""
]
},
{
"cell_type": "code",
"execution_count": 323,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 323,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"if not False:True"
]
},
{
"cell_type": "code",
"execution_count": 324,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\n",
"end\n"
]
}
],
"source": [
"if True:\n",
" print(\"Hello\")\n",
"elif True:\n",
" print(\"Bonjour\")\n",
"else: \n",
" print(\"Bom dia\")\n",
"print('end')"
]
},
{
"cell_type": "code",
"execution_count": 325,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bonjour\n",
"end\n"
]
}
],
"source": [
"if False:\n",
" print(\"Hello\")\n",
"elif True:\n",
" print(\"Bonjour\")\n",
"else: \n",
" print(\"Bom dia\")\n",
"print('end')"
]
},
{
"cell_type": "code",
"execution_count": 326,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bom dia\n",
"end\n"
]
}
],
"source": [
"if False:\n",
" print(\"Hello\")\n",
"elif False:\n",
" print(\"Bonjour\")\n",
"else:\n",
" print(\"Bom dia\")\n",
"print('end')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"if 0:True # no output"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"if 1:True"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"if -1:True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Recursion end test:"
]
},
{
"cell_type": "code",
"execution_count": 339,
"metadata": {},
"outputs": [],
"source": [
"def factorial(n):\n",
" if n==1:\n",
" return 1\n",
" return n*factorial(n-1)"
]
},
{
"cell_type": "code",
"execution_count": 340,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"720"
]
},
"execution_count": 340,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"factorial(6)"
]
},
{
"cell_type": "code",
"execution_count": 341,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"recursion error\n"
]
}
],
"source": [
"try:\n",
" factorial(0)\n",
"except RecursionError:\n",
" print(\"recursion error\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def factorial(n):\n",
" if n==0:\n",
" return 1\n",
" if n>0:\n",
" return n*factorial(n-1)\n",
" return n*factorial(n+1) # for negative number"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"-6"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"24"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"24"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"factorial(0)\n",
"factorial(1)\n",
"factorial(-1)\n",
"factorial(2)\n",
"factorial(-2)\n",
"factorial(3)\n",
"factorial(-3)\n",
"factorial(4)\n",
"factorial(-4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Modal logic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Non commutative `and`:"
]
},
{
"cell_type": "code",
"execution_count": 346,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 346,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=0\n",
"x!=0 and 1/x # no error\n",
"#1/x and x!=0 # error"
]
},
{
"cell_type": "code",
"execution_count": 347,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.1"
]
},
"execution_count": 347,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=10\n",
"x!=0 and 1/x"
]
},
{
"cell_type": "code",
"execution_count": 348,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 348,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True or 1/0"
]
},
{
"cell_type": "code",
"execution_count": 349,
"metadata": {},
"outputs": [],
"source": [
"def factorial(n):\n",
" return n<=1 or n*factorial(n-1)"
]
},
{
"cell_type": "code",
"execution_count": 350,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"120"
]
},
"execution_count": 350,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"factorial(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### For"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"for uses a global variable:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 3 2 "
]
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i='some important value'\n",
"for i in (1,3,2):\n",
" print(i,end=' ')\n",
"i # important value lost"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Don't do that in IPython (but OK in Python):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# for _ in (1,2,3):pass # breaks IPython history"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### While"
]
},
{
"cell_type": "code",
"execution_count": 351,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 1 2 3 4 5 6 7 8 9 --- 10\n"
]
}
],
"source": [
"i=0\n",
"while i<10:print(i,end=' ');i+=1\n",
"print(\"---\",i)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"init_cell": true
},
"outputs": [],
"source": [
"def range1(n):\n",
" i=0;\n",
" r=();\n",
" while ibreak allows to exit loop exactly where needeed (not necessarily at while): "
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 0, 1, 2, 3, 3, 4, 5, 5]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list2=[3,3,5,8,10,10,13,15,15]\n",
"i=0\n",
"downshift=list2[0]\n",
"while True:\n",
" list2[i]-=downshift\n",
" if i>=len(list2)-1: # last element\n",
" break\n",
" if list2[i+1]-downshift>list2[i]+1:\n",
" downshift+=list2[i+1]-downshift-(list2[i]+1)\n",
" i+=1\n",
"list2"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 0, 1, 2, 3, 3, 4, 5, 5]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list2=[3,3,5,8,10,10,13,15,15]\n",
"i=0\n",
"downshift=list2[0]\n",
"while True:\n",
" list2[i]-=downshift\n",
" if i>=len(list2)-1: # last element\n",
" break\n",
" downshiftincrease=list2[i+1]-downshift-(list2[i]+1)\n",
" if downshiftincrease>0:\n",
" downshift+=downshiftincrease\n",
" i+=1\n",
"list2"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 0, 1, 2, 3, 3, 4, 5, 5]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list2=[3,3,5,8,10,10,13,15,15]\n",
"i=0\n",
"downshift=list2[0]\n",
"while True:\n",
" list2[i]-=downshift\n",
" if i>=len(list2)-1: # last element\n",
" break\n",
" downshift+=max(list2[i+1]-downshift-(list2[i]+1),0)\n",
" i+=1\n",
"list2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iterable types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Range"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7]"
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(range(8))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"range does not immediately evaluate to a tuple or list of numbers:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"range(0, 1000000000)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n=1000000000;range(n)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"range"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(range(n))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# len(list(range(n)))==n # kernel dies, list is probably generated"
]
},
{
"cell_type": "code",
"execution_count": 378,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 4 5 6 7 \n",
"\n",
"0 2 4 6 \n",
"\n",
"8 7 6 5 4 3 2 1 "
]
}
],
"source": [
"for i in range(3,8):\n",
" print(i,end=' ');\n",
"print(\"\\n\")\n",
"\n",
"for i in range(0,8,2):\n",
" print(i,end=' ')\n",
"print(\"\\n\")\n",
"\n",
"for i in range(8,0,-1):\n",
" print(i,end=' ')"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"range(3, 5)"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"range(7, 2, -1)"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"range(3,8)[0]\n",
"range(3,8)[0:2]\n",
"range(3,8)[::-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A type is iterable if it can be used like `range` in `for`:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 3 2 \n",
"\n",
"R o b e r t \n",
"\n",
"1 3 1 2 \n",
"\n",
"1 2 3 \n",
"\n",
"Robert 19450510\n",
"Géal 19181111\n"
]
}
],
"source": [
"for i in (1,3,2):\n",
" print(i,end=' ')\n",
"print(\"\\n\")\n",
"\n",
"for i in \"Robert\":\n",
" print(i,end=' ')\n",
"print(\"\\n\")\n",
"\n",
"for i in [1,3,1,2]:\n",
" print(i,end=' ')\n",
"print(\"\\n\")\n",
"\n",
"for i in {1,3,2}:\n",
" print(i,end=' ')\n",
"print(\"\\n\")\n",
"\n",
"birthdates={'Robert':'19450510','Géal':'19181111'}\n",
"for i in birthdates:\n",
" print(i,birthdates[i])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Iterator, next"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [],
"source": [
"iter1=iter(['a', 'e', 'i', 'o', 'u'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# len(iter1) # error: has no length"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
"# iter1[2] # error: not subscriptable\\"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"# for i in iter1:print(next(iter1)) # try"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a e i o u "
]
}
],
"source": [
"for i in range(5):print(next(iter1),end=\" \")"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"u\n"
]
}
],
"source": [
"print(next(iter1)) # try again until error"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a e i o u "
]
}
],
"source": [
"iter1=iter(['a', 'e', 'i', 'o', 'u'])\n",
"try:\n",
" while True: \n",
" print(next(iter1),end=\" \") # try again until error\n",
"except StopIteration:pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Non reusable (hence immutable):"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [],
"source": [
"# next(iter1) # error: iterable exhausted"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generator, yield"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'whatever'"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i= 'whatever'\n",
"generator1=(i*i for i in range(4))\n",
"i"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"generator"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(generator1)"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"4\n",
"9\n"
]
}
],
"source": [
"for i in generator1:print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Non reusable :"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"for i in generator1:print(i) # prints nothing yet no error"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"ein.tags": "worksheet-0",
"jupyter": {
"outputs_hidden": true
},
"slideshow": {
"slide_type": "-"
}
},
"source": [
"yield instead of return in def returns a generator:"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 5 8 13 21 34 55 89 \n",
"\n",
"1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 "
]
}
],
"source": [
"def Fibonacci_generator(n):\n",
" a,b=0,1\n",
" for i in range(n):\n",
" a,b=b,a+b\n",
" yield b\n",
"\n",
"for i in Fibonacci_generator(10):print(i,end=' ')\n",
"print(\"\\n\")\n",
" \n",
"for i in Fibonacci_generator(20):print(i,end=' ')"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {
"init_cell": true,
"scene__Default Scene": true,
"tags": [
"ActiveScene"
]
},
"outputs": [],
"source": [
"from random import random\n",
"def random_batch(n):\n",
" for i in range(n):yield random()"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0.6202258932260998, 0.5257701556605389, 0.7918903364242222]"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(random_batch(3))"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {
"init_cell": true,
"scene__Default Scene": true,
"tags": [
"ActiveScene"
]
},
"outputs": [],
"source": [
"def addTrailingZero(iter1,n):\n",
" return iter1+type(iter1)(0 for i in range(n))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 0, 0, 0, 0]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"(0, 1, -2, 0, 0, 0, 0)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"(0, 1, -2)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"(0, 1, 0)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"addTrailingZero([0,1],4)\n",
"addTrailingZero((0,1,-2),4)\n",
"addTrailingZero((0,1,-2),0)\n",
"addTrailingZero((0,1,0),-1)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Unpacking"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See https://stackabuse.com/unpacking-in-python-beyond-parallel-assignment/."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2\n",
"2 1\n"
]
}
],
"source": [
"x,y=[1,2]\n",
"print(x,y)\n",
"x,y=y,x\n",
"print(x,y)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 1 2\n"
]
}
],
"source": [
"x,y,z=range(3)\n",
"print(x,y,z)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 1 2\n"
]
}
],
"source": [
"(x,y,z)=range(3)\n",
"print(x,y,z)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1] 2\n"
]
}
],
"source": [
"(*x,y)=range(3)\n",
"print(x,y)"
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"x,=[1]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def f1(*x):return len(x)\n",
"f1(1,2,3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Functional programming"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See this more elaborate presentation: https://docs.python.org/3.6/howto/functional.html."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Functional programming is programming without variables. It is not only possible but practical, because it avoids problems with variables."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constant"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Constant is allowed in functional programming. How to make a constant:"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"from collections import namedtuple"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"constants=namedtuple('Constants', ['h', 'e'])(6.626e-34,1.60217e-19)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"1.60217e-19"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"constants.e"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# constants.e=1.6e-19 # error"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pure function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A pure function is a function that work like a mathematical function, that is a univoque relation $x \\rightarrow f(x)$ or $(λ\\ x.f[x])$. It is a function that is not a procedure. Functional programming allows only pure function; moreover, such function should not have local variable, though it can use constant."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### λ or anonymous function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"x in lambda is never defined or \"dummy\": "
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"'whatever'"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x='whatever'\n",
"(lambda x:x**2)(10) \n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`x` in `lambda` is actually a constant (although it is often called variable in mathematics):"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"# (lambda x:x+=2;x**2)(10) # error"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"function"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(lambda x:x**2)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"21"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(lambda x,y:x+y)(10,11)\n",
"# (lambda x,y:x+y)[10,11] # error"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"21"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(lambda x:x[0]+x[1])([10,11])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Functional forms"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Inline `if`:"
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 164,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 164,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1+1 if True else 10\n",
"1+1 if False else 1\n"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def abs(x):return x if x>=0 else -x\n",
"abs(-3)\n",
"abs(3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`1/0` is held:"
]
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 171,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1/0 if False else 0"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 165,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 165,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def fif(x,tc,fc):return tc if x else fc\n",
"1+fif(True,1,10)\n",
"1+fif(False,1,10)"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"fpos=(lambda x:fif(x>0,x,0))\n",
"fneg=(lambda x:fif(x>0,0,-x))"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[(True, True), (True, True), (True, True)]"
]
},
"execution_count": 168,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list((abs(x)==fpos(x)+fneg(x),x==fpos(x)-fneg(x)) for x in (-3,0,3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However `fif` evaluates its arguments:"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# fif(False,1/0,0) # error: ZeroDivisionError"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {
"init_cell": true,
"scene__Default Scene": true,
"tags": [
"ActiveScene"
]
},
"outputs": [],
"source": [
"def fand(*iter1):\n",
" return set(iter1)=={True} or set(iter1)==set()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fand()\n",
"fand(True)\n",
"fand(True,False,True)\n",
"fand(True,True,True)\n",
"fand(True,True,False)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"# fand(False,1/0) # error: division by zero"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# all() # error\n",
"all([True,False,True])\n",
"all([True,True,True])"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"# all(False,1/0) # error: division by zero"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def ftimes(*iter1):\n",
" y=1\n",
" for i in range(len(iter1)):\n",
" y*=iter1[i]\n",
" return y"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"-8"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"60"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ftimes(2,-4)\n",
"ftimes(3,4,5)\n",
"ftimes(4)\n",
"ftimes()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pattern matching"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Signature:"
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [],
"source": [
"def function1(x:float,y:int)->float:return x,y"
]
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [],
"source": [
"# help(function1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unfortunately, argument type specification is just for `help`, not actual pattern matching:"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(4.0, 2)"
]
},
"execution_count": 185,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"(4, 2.0)"
]
},
"execution_count": 185,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"('x', 2)"
]
},
"execution_count": 185,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function1(4.,2)\n",
"function1(4,2.)\n",
"function1(\"x\",2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In particular, it is not possible to define a function only for a given argument:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# def factorial(0):return 1 # error"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pattern matching can be replaced by testing, for example, with filter, like Wolfram language Select:"
]
},
{
"cell_type": "code",
"execution_count": 186,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"['w']"
]
},
"execution_count": 186,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(filter(lambda x:type(x)==str,[\"w\",(1),[2],4]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python 3.10 has pattern matching:"
]
},
{
"cell_type": "code",
"execution_count": 203,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'integer'"
]
},
"execution_count": 203,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"'string'"
]
},
"execution_count": 203,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def f1(n):\n",
" match n:\n",
" case int(_):return 'integer'\n",
" case str(_):return 'string'\n",
"f1(2)\n",
"f1('toto')"
]
},
{
"cell_type": "code",
"execution_count": 205,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'f1(2)'"
]
},
"execution_count": 205,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def f1(n):\n",
" match n:\n",
" case 2:return 'f1(2)'\n",
" case 3:return 'f1(3)'\n",
"f1(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## List manipulation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Transpose**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# zip?"
]
},
{
"cell_type": "code",
"execution_count": 210,
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 3, 5), (2, 4, 6)]"
]
},
"execution_count": 210,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"[(1, 3, 5), (2, 4, 6)]"
]
},
"execution_count": 210,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"[(1, 3, 5), (2, 4, 6)]"
]
},
"execution_count": 210,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"[(1, 3, 5), (2, 4, 6)]"
]
},
"execution_count": 210,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(zip(*[[1,2],[3,4],[5,6]]))\n",
"list(zip(*([1,2],[3,4],[5,6])))\n",
"list(zip(*((1,2),(3,4),(5,6))))\n",
"list(zip(*[(1,2),[3,4],[5,6]]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Applying function to list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See https://reference.wolfram.com/language/guide/ApplyingFunctionsToLists.html."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Apply** "
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Type error.\n"
]
},
{
"data": {
"text/plain": [
"(1, 2)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"(1, 2)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def diadic(x,y): \n",
" return x,y\n",
"\n",
"try: \n",
" diadic([1,2]) \n",
"except TypeError:\n",
" print(\"Type error.\")\n",
"\n",
"diadic(*[1,2]) \n",
"diadic(*(1,2))"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 2)"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divmod(*[17,5])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Map**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is more clear with a function that does not evaluate:"
]
},
{
"cell_type": "code",
"execution_count": 211,
"metadata": {},
"outputs": [],
"source": [
"from sympy import Function\n",
"f1=Function('f1')"
]
},
{
"cell_type": "code",
"execution_count": 214,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[f1(-1), f1(3), f1(4)]"
]
},
"execution_count": 214,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"(f1(-1), f1(3), f1(4))"
]
},
"execution_count": 214,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# map(f1,[-1,3,4])\n",
"list(map(f1,[-1,3,4]))\n",
"tuple(map(f1,(-1,3,4)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**MapIndexed**"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 'a'), (1, 'b'), (2, 'c')]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(enumerate('abc'))"
]
},
{
"cell_type": "code",
"execution_count": 212,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[f1((0, a)), f1((1, b)), f1((2, c))]"
]
},
"execution_count": 212,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(f1,enumerate('abc')))"
]
},
{
"cell_type": "code",
"execution_count": 215,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[f1(0, a), f1(1, b), f1(2, c)]"
]
},
"execution_count": 215,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda x:f1(*x),enumerate('abc')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**MapThread**"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[f1(1, 4), f1(2, 5), f1(3, 6)]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda x:f1(*x),zip([1,2,3],[4,5,6])))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Inner**"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle f_{1}{\\left(1,4 \\right)} + f_{1}{\\left(2,5 \\right)} + f_{1}{\\left(3,6 \\right)}$"
],
"text/plain": [
"f1(1, 4) + f1(2, 5) + f1(3, 6)"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(map(lambda x:f1(*x),zip([1,2,3],[4,5,6])))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Outer**"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[f1(10, 1), f1(10, 2), f1(10, 3)]"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda y:f1(10,y),[1,2,3]))"
]
},
{
"cell_type": "code",
"execution_count": 217,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[[f1(10, 1), f1(10, 2), f1(10, 3)],\n",
" [f1(20, 1), f1(20, 2), f1(20, 3)],\n",
" [f1(30, 1), f1(30, 2), f1(30, 3)]]"
]
},
"execution_count": 217,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda x:list(map(lambda y:f1(x,y),[1,2,3])),[10,20,30]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A large functional expression can be read or edited more easily with a tool for selecting functional subexpressions at any le\n",
"vel (none exists in Jupyter Notebook)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using objects"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Identity, value"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Objects created identically may or may not be same:"
]
},
{
"cell_type": "code",
"execution_count": 242,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 242,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"math is math"
]
},
{
"cell_type": "code",
"execution_count": 250,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 250,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"range(4) is range(4)\n",
"# 44 is 44 # error"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Same names recall the same object:"
]
},
{
"cell_type": "code",
"execution_count": 269,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 269,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=range(4)\n",
"x is x"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Each object has an identity number, guaranteed to be unique among simultaneously existing objects. Two objects are same if and only if thay have the same identity number."
]
},
{
"cell_type": "code",
"execution_count": 263,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 263,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"140244541075072"
]
},
"execution_count": 263,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"140244541075456"
]
},
"execution_count": 263,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(range(4))==id(range(4)) # ids cannot be compared like this on the left\n",
"id(range(4))\n",
"id(range(4))"
]
},
{
"cell_type": "code",
"execution_count": 271,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8911176"
]
},
"execution_count": 271,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"8911176"
]
},
"execution_count": 271,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"140244235327376"
]
},
"execution_count": 271,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"140244235326384"
]
},
"execution_count": 271,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"140244235326800"
]
},
"execution_count": 271,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"140244235326800"
]
},
"execution_count": 271,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(44)\n",
"id(44)\n",
"\n",
"id(444)\n",
"id(444)\n",
"\n",
"x=444\n",
"id(x)\n",
"id(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Every object as a value. "
]
},
{
"cell_type": "code",
"execution_count": 267,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"